function spike_analysis(varargin)
% spike analysis - 
% if no spikes have been analyzed, this routine does it and stores the data into
% the control worksheet. (CONTROL(sf).spike).
% 
% based on:
% basic template file for data analysis routines.
% gets the current datac window selection, and does the analysis
% for all selected record groups/blocks
% automatically refreshes the data arrays from the disk file if necessary
% and also shows how to generate a figure.
% 8/23/05 P. Manis

% the main routine is just a wrapper to handle the multiple selection and
% catch execution errors.
%
% Note: when testing, set the testflag to 1 so that we DO NOT do try/catch.
% you need to do this to catch the errors in the _go routine.
%
global CONTROL

if(nargin == 0)
    noq = 1;
else
    noq = varargin{1};
end;

sf = getmainselection; % read the list of selected traces
testflag = 1;

if(sf > 0)
    pflag = getplotflag; % read teh plot flag (used in the routine template2)
    
    QueMessage('Spike time analysis routine', 1); % clear the que
    for i = 1:length(sf)
        if(noq == 0)
            CONTROL(sf(i)).basechan = []; % clear the base channel for analysis...
        end;
        if(testflag == 0)
            try
                template_go(sf(i), pflag, noq);
            catch
                fprintf(1, 'Spike time Analysis Failed execution for sf = %d\n  ***[File: %s, records: %d - %d ]\n', ...
                    sf(i), CONTROL(sf(i)).filename, CONTROL(sf(i)).recbeg, CONTROL(sf(i)).recend);
            end;
        else
            template_go(sf(i), 0, noq); % also turn off plot flag when testing
        end;

    end;
end;



%--------------The real analysis routine------------------

function template_go(sf, pflag, noq)

% access to all of the data and parameters
global DFILE ALLCH VOLTAGE CONTROL

% only do the analysis if:
% 1. the structure does not already exist,
% and
% 2. the file is known to be on the path...
%
if(CONTROL(sf).fileinpath ==0) % skip files not in path
    QueMessage(sprintf('File not in path: %s', CONTROL(sf).filename));
    return;
end;
% set some control parameters
isrc = 1;
t0 = 0;
tmax = 5000;
spike_thresh = CONTROL(sf).thresh;
if(noq == 0)
    if(isempty(CONTROL(sf).basechan))
        isrc = 1;
        prompt={'Base Channel:','Spike Threshold:'};
        def={num2str(isrc),num2str(spike_thresh)};
        dlgTitle='Spike analysis Parameters';
        lineNo=1;
        answer=inputdlg(prompt,dlgTitle,lineNo,def);
        isrc = str2num(answer{1});
        CONTROL(sf).basechan = isrc;
        spike_thresh = str2num(answer{2});
        CONTROL(sf).thresh=spike_thresh;
    end;
else
    spike_thresh = CONTROL(sf).thresh;
    isrc = CONTROL(sf).basechan;
end;

if(ischar(isrc))
    isrc = str2num(isrc);
end;

%if(check_field(CONTROL(sf), 'spike') && ~isempty(CONTROL(sf).spike)) % must already be done
%    QueMessage(sprintf('File already analyzed: %s', CONTROL(sf).filename));
%    if(CONTROL(sf).G1 == 0)
%        CONTROL(sf).G1 = 1; % make sure it shows...
%    end;
%    return;
%end;


% this next routine reads all of the data and information we need,
% including calculating some standard stuff that is put into DPAR.
% you will want DPAR.time especially - it is the time base that goes
% with the data.

[DFILE, DPAR, err] = ana_setup(DFILE, sf);
if(err ~= 0)
    return;
end;
tmax = max(max(DPAR.time))-DPAR.RATES(1);

 if(~isempty(ALLCH))
     V = ALLCH{isrc};
%     CURRENT = ALLCH{isrc+1};
 end;


% the analysis here is just finding spikes

[fsl, first_isi, nr_spikes, spike_train]=find_spikes2(DFILE, V, t0, tmax, spike_thresh);



spike.isrc = isrc;
spike.fsl = fsl;
spike.latency = {spike_train.latency};
spike.source = {spike_train.source};
CONTROL(sf).spike = spike;
    if(CONTROL(sf).G1 == 0)
        CONTROL(sf).G1 = 1; % make sure it shows...
    end;
setlist(sf);
sl=spike(:).latency;
z=0;
for i = 1:length(sl)
    z = z+sum(length(sl{i}));
end;
QueMessage(sprintf('Spike analysis - found %d spikes', z));

